# 这是离起始点的距离，单位 m
xAxisData = [
    0.0,
    2.2,
    9.5,
    16.0,
    19.3,
    21.5,
    23.9,
    25.0,
    25.1,
    28.0,
    30.0,
    32.0,
    35.0,
    38.0,
    41.0,
    44.0,
    46.0,
    48.0,
    50.0,
    52.0,
    54.0,
    56.0,
    58.0,
    60.0,
    63.0,
    66.0,
    70.0,
    75.0,
    76.5,
    79.9,
    82.6,
    91.0,
    99.4,
    103,
]

# 这是对应起始点的高程，单位 m
yAxisData = [
    732.53,
    732.33,
    728.15,
    727.79,
    727.74,
    726.64,
    725.44,
    725.45,
    724.78,
    724.12,
    724.38,
    724.18,
    724.43,
    724.38,
    724.58,
    724.62,
    724.56,
    724.52,
    724.58,
    724.55,
    724.52,
    724.42,
    724.32,
    724.10,
    724.13,
    724.13,
    724.23,
    724.38,
    724.81,
    726.64,
    728.05,
    728.42,
    729.16,
    732.08,
]

# 水位流量关系曲线
Level_Runoff = {
    '725.8':[0   ,0   ,0   ,0   ,0   ,0   ,0   ,0   ,0   ,106 ],
    '725.9':[107 ,108 ,109 ,110 ,112 ,113 ,115 ,116 ,117 ,119 ],

    '726.0':[120 ,121 ,123 ,124 ,126 ,128 ,129 ,130 ,132 ,134 ],
    '726.1':[135 ,136 ,138 ,140 ,141 ,142 ,144 ,145 ,147 ,148 ],
    '726.2':[150 ,152 ,153 ,155 ,157 ,158 ,160 ,162 ,163 ,165 ],
    '726.3':[167 ,169 ,171 ,172 ,174 ,176 ,178 ,179 ,181 ,183 ],
    '726.4':[185 ,187 ,189 ,191 ,192 ,194 ,196 ,198 ,200 ,202 ],
    '726.5':[204 ,206 ,208 ,210 ,212 ,214 ,216 ,218 ,220 ,222 ],
    '726.6':[224 ,226 ,228 ,230 ,232 ,234 ,236 ,238 ,240 ,242 ],
    '726.7':[244 ,246 ,248 ,250 ,253 ,255 ,257 ,259 ,262 ,264 ],
    '726.8':[266 ,268 ,271 ,273 ,275 ,278 ,280 ,282 ,284 ,287 ],
    '726.9':[289 ,291 ,294 ,296 ,298 ,300 ,303 ,305 ,307 ,310 ],

    '727.0':[312 ,314 ,317 ,319 ,321 ,324 ,326 ,329 ,331 ,334 ],
    '727.1':[336 ,339 ,341 ,344 ,346 ,349 ,352 ,354 ,357 ,359 ],
    '727.2':[362 ,365 ,367 ,370 ,372 ,375 ,378 ,380 ,383 ,385 ],
    '727.3':[388 ,391 ,393 ,396 ,398 ,401 ,404 ,406 ,409 ,411 ],
    '727.4':[414 ,417 ,419 ,422 ,424 ,427 ,430 ,432 ,435 ,437 ],
    '727.5':[440 ,443 ,445 ,448 ,450 ,453 ,456 ,458 ,461 ,463 ],
    '727.6':[466 ,469 ,471 ,474 ,476 ,479 ,482 ,484 ,487 ,    ],

}

class RiverSpeed():
    def __init__(self, height):
        self.height = height
        self.xAxisData = xAxisData
        self.yAxisData = yAxisData
        self.area = self.get_runoff()
        self.speed = self.get_speed()                        

    def get_speed(self):
        level_str = self.height.__str__()
        level_str = level_str if level_str.__len__()==6 else level_str + '0'
        self.runoff = Level_Runoff[level_str[:5]][int(level_str[5])]
        speed = self.runoff/self.area
        return speed

    # 计算河道横截面的面积
    def get_runoff(self):
        # 1.首先计算左右两边三角形的面积
        height = self.height
        #   利用两点计算斜率k = (y2-y1)/(x2-x1)
        x_left = 0
        x_right = 0
        river_split_area = 0
        river_split_runoff = 0
        for i in range(1,len(self.yAxisData)):
            # 首先要根据height找到对应的左点坐标
            if (self.yAxisData[i - 1] > height and self.yAxisData[i] < height):
                x1 = self.xAxisData[i - 1]
                y1 = self.yAxisData[i - 1]
                x2 = self.xAxisData[i]
                y2 = self.yAxisData[i]
                k = (y2 - y1) / (x2 - x1)
                b = y1 - k * x1
                x_left = (height - b) / k
                # console.log(x_left)

                # 找到之后计算右边的高度和底长，以此计算面积
                top_len = self.xAxisData[i] - x_left
                bottom_len = height - self.yAxisData[i]
                area = (top_len * bottom_len) / 2
                river_split_area += area
                break
            
        for i in range(len(self.yAxisData) - 1,0,-1):
            # 首先要根据height找到对应的右点坐标
            if (self.yAxisData[i] > height and self.yAxisData[i - 1] < height):
                x1 = self.xAxisData[i - 1]
                y1 = self.yAxisData[i - 1]
                x2 = self.xAxisData[i]
                y2 = self.yAxisData[i]
                k = (y2 - y1) / (x2 - x1)
                b = y1 - k * x1
                x_right = (height - b) / k
                # console.log(x_right)

                # 找到之后计算左边的高度和底长，以此计算面积
                top_len = x_right - self.xAxisData[i - 1]
                bottom_len = height - self.yAxisData[i - 1]
                area = (top_len * bottom_len) / 2
                river_split_area += area
                break
            
        
        # 找到 小于水面高程的河道高程点，应用梯形法则
        for i in range(len(self.yAxisData) - 1):
            if (self.yAxisData[i] < height and self.yAxisData[i + 1] < height):
                top_len = height - self.yAxisData[i]
                bottom_len = height - self.yAxisData[i + 1]
                dis_top_bottom = self.xAxisData[i + 1] - self.xAxisData[i]
                area = (top_len + bottom_len) * dis_top_bottom / 2
                river_split_area += area
                # console.log(area)
            
        # console.log(river_split_area)
        # area = river_split_area
        return river_split_area
    
